package cn.vivi.order.controller;

import cn.vivi.commons.entity.Payment;
import cn.vivi.commons.util.Result;
import cn.vivi.order.client.PaymentClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("orders")
@Slf4j
@RefreshScope // 动态刷新
public class OrderController {

    private static final String PARENT_URL = "http://CLOUD-PAYMENT-SERVICE";

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private PaymentClient paymentClient;

    @PostMapping
    public Result createOrder(@RequestBody Payment payment) {
        return restTemplate.postForObject(PARENT_URL + "/payments", payment, Result.class);
    }

    @GetMapping("{id}")
    public Result getPayment(@PathVariable String id) {
        return restTemplate.getForObject(PARENT_URL + "/payments/" + id, Result.class);
    }

    @GetMapping("/feign/{id}")
    public Result getPaymentByFeign(@PathVariable String id) {
        log.info("hello feign@@@");
        return paymentClient.findPayment(id);
    }

}
